package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import eproject.mm.com.projectsupervision.PrjApplication;
import eproject.mm.com.projectsupervision.R;

public class ServerSettingActivity extends Activity {
    EditText et_serverIp,et_port;
    Button btn_save;
    SharedPreferences userSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server_setting);

        btn_save = (Button)findViewById(R.id.btn_save);

        et_serverIp = (EditText)findViewById(R.id.et_serverIp);
        et_port = (EditText)findViewById(R.id.et_port);

        loadData();
        initEvent();
    }

    private void loadData(){
        userSettings = getSharedPreferences("setting", 0);
        String serverip = userSettings.getString("serverip","47.104.65.54");
        String serverport = userSettings.getString("serverport","8011");

        et_serverIp.setText(serverip);
        et_port.setText(serverport);
    }

    private void initEvent(){
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String serverip = et_serverIp.getText().toString().trim();
                String serverport = et_port.getText().toString().trim();

                SharedPreferences.Editor editor = userSettings.edit();
                editor.putString("serverip", serverip);
                editor.putString("serverport", serverport);

                editor.commit();

                Intent intent=new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("serverip", serverip);
                bundle.putString("serverport", serverport);
                intent.putExtras(bundle);

                setResult(RESULT_OK, intent);

                finish();
            }
        });
    }
    public void back(View view) {
        finish();
    }
}
